﻿// thread_2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <windows.h>
#include <functional>
#include "Thread.h"
using namespace std;

class MyClass
{
public:
	MyClass();
	~MyClass();
	string str = "MyClass test";
private:

};

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}

class MyThread :public CThread
{
public:
	MyThread();
	virtual ~MyThread();
	virtual void Run() override;

};

MyThread::MyThread()
{
}

MyThread::~MyThread()
{
}

void MyThread::Run()
{
	while (this->IsInterrupted())
	{
		std::cout << "MyThread::Run()" << std::endl;
		Sleep(1000);
	}
	return;
}

class MyThread_B
{
public:
	MyThread_B();
	virtual ~MyThread_B();
	void Run();
	CThread pThread;
	char* m_pBuffer;
};

MyThread_B::MyThread_B()
{
	m_pBuffer = new char[8];
	memset(m_pBuffer, 'A', 7);
	m_pBuffer[7] = '\0';
	MyClass *pMyClass = new MyClass;
//	std::function<int()> func1 = std::bind(&MyThread_B::Run, this);
	auto func1 = std::bind(&MyThread_B::Run, this);
	pThread.Start(func1, this);
	std::cout << "MyThread_B()" << std::endl;
}

MyThread_B::~MyThread_B()
{
	std::cout << "~MyThread_B()" << std::endl;
}

void MyThread_B::Run()
{
	while (pThread.IsInterrupted())
	{
		std::cout << "MyThread_B::Run() :" << m_pBuffer << std::endl;
		Sleep(1000);
	}
	return;
}

int TestRun(void* arg)
{
	CThread thre;
	while (thre.IsInterrupted())
	{
		std::cout << "TestRun()" << std::endl;
		Sleep(1000);
	}

	return 0;
}

CThread thre_1;
int TestRun_1(void* arg)
{
	printf(":%s\n",(char*)arg);
	while (thre_1.IsInterrupted())
	{
		std::cout << "TestRun_1()" << std::endl;
		Sleep(1000);
	}
	return 0;
}

int main()
{
#if 0
	MyThread_B *thre = new MyThread_B();
	Sleep(5000);
	delete thre;
#endif

#if 0
	CThread thre;
	int i = 5;
	while (i--)
	{
		thre.Start(TestRun);
		Sleep(5000);
		thre.Stop();
	}

#endif

#if 1
	//int a = 1;
	//void* arg = a;
	char* arg = (char*)"abc";
	
	thre_1.Start(TestRun_1, (void*)arg);
#endif

#if 0
	//继承
	int n = 5;
	MyThread th;
	while (n--)
	{
		th.Start();

		Sleep(5000);
		th.Stop();
	}
#endif



    while (true)
    {
//		std::cout << "main thread id:" << std::this_thread::get_id() << std::endl;
        Sleep(1000);
    }
    return 0;
}